package com.bkhc.business.service;

import com.bkhc.business.entity.BusinessPriceAccount;
import com.bkhc.business.entity.Group;
import com.bkhc.business.entity.Member;
import com.bkhc.business.entity.vo.Order;
import com.bkhc.business.repository.BusinessPriceAccountRepository;
import com.bkhc.business.repository.GroupRepository;
import com.bkhc.business.repository.MemberRepository;
import com.bkhc.business.repository.ProductRepository;
import com.bkhc.common.constant.OrderStatusConstant;
import com.bkhc.common.repository.UmcRepository;
import com.bkhc.common.to.ProductPriceTo;
import com.bkhc.common.util.CommonUtil;
import com.bkhc.common.util.response.PageUtil;
import com.bkhc.common.util.response.ResultJson;
import com.bkhc.common.vo.UserBean;
import org.apache.commons.lang.StringUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class CommonService {

    @Resource
    UmcRepository umcRepository;

    @Resource
    GroupRepository groupRepository;

    @Resource
    MemberRepository memberRepository;

    @Resource
    ProductRepository productRepository;

    @Autowired
    BusinessPriceAccountRepository accountRepository;

    /**
     * 根据条件查询产品价格表
     *
     * @param
     * @param pageIndex
     * @param pageSize
     * @return
     */
    public ResultJson<?> queryProductPriceByVo(int pageIndex, int pageSize) {
        //获取当前登录用户，根据用户角色是否为业务合伙人以及业务员来决定是否查看所有产品价格还是单一产品价格
        UserBean crruentUser = CommonUtil.getCrruentUser();
        String userId = crruentUser.getUserId();
        Member member = memberRepository.findByUserId(userId);
        if (member == null) {
            //如果不是业务员 TODO
        }
        String productCodes = null;
        String priceTypes = null;
        List<ProductPriceTo> goodsPrice = new ArrayList<>();
        if (member.isLeader()) {
            //获取分组所有产品code
            String id = member.getId()+"";
            Optional<Group> groupOptional = groupRepository.findById(id);
            Group group = groupOptional.get();
            if (group != null) {
                productCodes = group.getProductCodes();
            }
        } else {
            Sort.Order orders = new Sort.Order(Sort.Direction.DESC, "updateDate");
            PageRequest pageRequest = PageRequest.of(pageIndex - 1, pageSize, Sort.by(orders));
            BusinessPriceAccount priceAccount = new BusinessPriceAccount();
            priceAccount.setMemberId(member.getId()+"");
            Page<BusinessPriceAccount> priceAccounts = accountRepository.findAll(priceAccount, pageRequest);
            List<BusinessPriceAccount> accountList = priceAccounts.getContent();
            goodsPrice.addAll(productRepository.getGoodsPriceByPriceCount(accountList));
            ResultJson result = ResultJson.success();
            result.setData(goodsPrice);
            return result;
            //String codes = member.getProductCodes();
            //priceTypes = member.getPriceTypes();
        }
        Map<String,String> paramMap = new HashMap<>();
        paramMap.put("productCode",productCodes);
        paramMap.put("priceType",priceTypes);
        if (StringUtils.isNotEmpty(productCodes)) {
            goodsPrice.addAll(productRepository.getGoodsPrice(paramMap,pageIndex,pageSize));
        }
        ResultJson result = ResultJson.success();
        result.setData(goodsPrice);
        return result;
    }

    /**
     * 根据业务员种类查看订单
     * @return
     * @param pageIndex
     * @param pageSize
     * @param order
     */
    public ResultJson<?> queryOrderItem(int pageIndex, int pageSize, Order order) {
        UserBean crruentUser = CommonUtil.getCrruentUser();
        String userId = crruentUser.getUserId();
        Member member = memberRepository.findByUserId(userId);
        if (member!=null){
            order.setCreator(crruentUser.getUserId());
            productRepository.getOrder(order,pageIndex,pageSize);
        }
        PageUtil page = new PageUtil();
        //如果是业务合伙人，可查看当前业务员提交的订单
        if (member.isLeader()) {
            String groupCode = member.getGroupCode();
            order.setBusinessGroup(groupCode);
            //order.setAuditStatus(OrderStatusConstant.BUSINESS_AGREE);
            page = productRepository.getOrder(order, pageIndex, pageSize);
        } else {
            //如果是业务员
            String id = member.getId()+"";
            order.setBusinessGroupMember(id);
            //order.setAuditStatus(OrderStatusConstant.CUSTOM_APPLY);
            page = productRepository.getOrder(order, pageIndex, pageSize);
        }
        ResultJson result = new ResultJson();
        result.setData(page);
        return result;

    }


}
